package com.kevin.ymqquan.application;

import java.util.LinkedList;
import java.util.List;

import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.conn.ClientConnectionManager;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.protocol.HTTP;


import android.app.Activity;
import android.app.Application;
import android.content.Context;

public class App extends Application {
	public static Context context;
	private HttpClient httpClient;

	/* ��Ļ�ֱ��� */
	private int widthPixels;
	private int heightPixels;
	/**
	 * �洢���activity���ƹر�
	 * */
	private List<Activity> activityList = new LinkedList<Activity>();
	private static App instance;

	// ����ģʽ�л�ȡΨһ��ExitApplicationʵ��
	public static App getInstance() {
		if (null == instance) {
			instance = new App();
		}
		return instance;
	}

	// ���Activity��������
	public void addActivity(Activity activity) {
		activityList.add(activity);
	}

	// ��������Activity��finish
	public void exit() {
		for (Activity activity : activityList) {
			activity.finish();
		}
		// System.exit(0);
	}

	public int userId = 0;
//	public int userType;
//	public String deviceId;

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();

		context = this.getApplicationContext();
		// ��ȡ�������Ӿ��
		httpClient = this.createHttpClient();
	}

	@Override
	public void onLowMemory() {
		super.onLowMemory();

	}

	@Override
	public void onTerminate() {
		super.onTerminate();
		this.shutdownHttpClient();
	}

	// ����HttpClientʵ��
	private HttpClient createHttpClient() {
		HttpParams params = new BasicHttpParams();
		HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
		HttpProtocolParams.setContentCharset(params,
				HTTP.DEFAULT_CONTENT_CHARSET);
		HttpProtocolParams.setUseExpectContinue(params, true);
		HttpConnectionParams.setConnectionTimeout(params, 20 * 1000);
		HttpConnectionParams.setSoTimeout(params, 20 * 1000);
		HttpConnectionParams.setSocketBufferSize(params, 8192);
		SchemeRegistry schReg = new SchemeRegistry();
		schReg.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		schReg.register(new Scheme("https",
				SSLSocketFactory.getSocketFactory(), 443));

		ClientConnectionManager connMgr = new ThreadSafeClientConnManager(
				params, schReg);

		return new DefaultHttpClient(connMgr, params);
	}

	// �ر����ӹ��������ͷ���Դ
	private void shutdownHttpClient() {
		if (httpClient != null && httpClient.getConnectionManager() != null) {
			httpClient.getConnectionManager().shutdown();
		}
	}

	// �����ṩHttpClientʵ��
	public HttpClient getHttpClient() {
		return httpClient;
	}

	public int getWidthPixels() {
		return widthPixels;
	}

	public void setWidthPixels(int widthPixels) {
		this.widthPixels = widthPixels;
	}

	public int getHeightPixels() {
		return heightPixels;
	}

	public void setHeightPixels(int heightPixels) {
		this.heightPixels = heightPixels;
	}
}
